var AutoPlayEvent = (function() {
	var AutoPlayEvent = function(options) {
		this.PLAY_DELAY = options.delay || 1500;
		this.MIN_INDEX = options.minIndex || 1;
		this.MAX_INDEX = options.maxIndex || 9;
		this.FIRE_PREFIX = options.firePrefix || "ev_";
		this.SHOW_PREFIX = options.showPrefix || "evt_";

		// 设置默认值
		this.autoPlay = options.autoPlay || false;
		this.intervalId = null;
		this.currIndex = 1;

		this.bindHoverEvent();
		if (this.autoPlay) {
			this.autoPlayEvent();
		}		
	};

	AutoPlayEvent.prototype = {
		// 绑定hover事件
		bindHoverEvent: function() {
			var _this = this;
			for (var i = _this.MIN_INDEX; i <= _this.MAX_INDEX; i++) {
				(function(j) {
					$("#" + _this.FIRE_PREFIX + j).hover(function() {
						_this.clearIntervalId();
						_this.hidePrev();

						$("#" + _this.SHOW_PREFIX + j).show();
					}, function() {
						$("#" + _this.SHOW_PREFIX + j).hide();
					});
				})(i);
			}
		},
		// 自动播放历程
		autoPlayEvent: function() {
			var _this = this;

			this.clearIntervalId();
			this.intervalId = setInterval(function() {
				_this.playEvent();
			}, this.PLAY_DELAY);
		},
		// 清除计时器
		clearIntervalId: function() {
			if (this.intervalId) {
				clearInterval(this.intervalId);
			}
		},
		// 播放历程
		playEvent: function() {
			this.hidePrev();
			this.showCurr();
		},
		// 隐藏前一个
		hidePrev: function() {
			var prevIndex = this.getPrevIndex();
			$("#" + this.SHOW_PREFIX + prevIndex).hide();
		},
		// 显示当前项
		showCurr: function() {
			$("#" + this.SHOW_PREFIX + this.currIndex).show();
			this.setCurrIndex();
		},
		// 获取前一个指针
		getPrevIndex: function() {
			var prevIndex = this.currIndex - 1;
			if (prevIndex < this.MIN_INDEX) {
				prevIndex = this.MAX_INDEX;
			}
			return prevIndex;
		},
		// 设置当前指针
		setCurrIndex: function() {
			this.currIndex++;
			if (this.currIndex > this.MAX_INDEX) {
				this.currIndex = this.MIN_INDEX;
			}
		}
	};

	// 静态方法
	AutoPlayEvent.create = function(options) {
		return new AutoPlayEvent(options);
	};

	return AutoPlayEvent;
})();